﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using ZHDJ.Data;
using ZHDJ.Data.Models.Org;

namespace ZHDJ.WebPage
{
    public class CurrentUserManager
    {
        private static PartyOrgMember _CurrentUser
        {
            get
            {
                return HttpContext.Current.Session["_CurrentUser_"] as PartyOrgMember;
            }
            set
            {
                HttpContext.Current.Session["_CurrentUser_"] = value;
            }
        }

        /// <summary>
        /// 当前登陆用户
        /// </summary>
        public static PartyOrgMember CurrentUser
        {
            get
            {
                using (var DB = new DBInstance().DB)
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        if (_CurrentUser != null)
                        {
                            return _CurrentUser;
                        }
                        var temp = DB.PartyOrgMembers.Find(Convert.ToString(HttpContext.Current.User.Identity.Name));
                        temp.ManageBranchIDs = new List<string>();
                        var dt = PartyOrg.GetBranchManageByLoginId(temp.LoginID);
                        foreach (DataRow dr in dt.Rows)
                        {
                            temp.ManageBranchIDs.Add(Convert.ToString(dr["BranchID"]));
                        }
                        _CurrentUser = temp;
                        return _CurrentUser;
                    }
                    return null;
                }
            }
        }
    }
}
